package com.ym.manager.admin.group;

import com.ym.datasource.DataSourceType;
import com.ym.datasource.UseDataSource;
import com.ym.utils.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 但何非
 * @Date 2022/11/12 21:15
 * @ClassName: GroupSerice
 * @Description:
 * @Version:
 */
@Service
public class GroupService {
    @Autowired
    private GroupMapper groupMapper;

    @UseDataSource(DataSourceType.YS_IM)
    public Pages<GroupInfo> getList(int start, int length, String name, int id, int status, int beginDay, int endDay) {
        Map<String,Object> map = new HashMap<>();
        map.put("begin",start);
        map.put("limit",length);
        map.put("id",id);
        map.put("name",name);
        map.put("status",status);
        int count  = groupMapper.getListCount(map);
        Pages<GroupInfo> pages = new Pages<>(count,start==0?1:start/length,length);
        List<GroupInfo> list = groupMapper.getList(map);
        for (GroupInfo groupInfo:list) {
            groupInfo.userCount = groupMapper.userCount(groupInfo.id);
            int groupPay = groupMapper.groupPayUserCount(groupInfo.id,beginDay,endDay);
            groupInfo.isPay = groupPay>0 ? true : false;
        }
        pages.setList(list);
        return pages;
    }

    @UseDataSource(DataSourceType.YS_IM)
    public Pages<GroupUserInfo> userList(long groupId,int start, int length, String nickName, String mobile, long uid, int status,int userStatus) {
        Map<String,Object> map = new HashMap<>();
        map.put("groupId",groupId);
        map.put("begin",start);
        map.put("limit",length);
        map.put("mobile",mobile);
        map.put("uid",uid);
        map.put("nickName",nickName);
        map.put("status",status);
        map.put("userStatus",userStatus);
        int count  = groupMapper.getUserListCount(map);
        Pages<GroupUserInfo> pages = new Pages<>(count,start==0?1:start/length,length);
        List<GroupUserInfo> list = groupMapper.getUserList(map);
        pages.setList(list);
        return pages;
    }

    @UseDataSource(DataSourceType.YS_IM)
    public int updateStatus(long id, int status) {
        return groupMapper.updateStatus(id,status);
    }

    @UseDataSource(DataSourceType.YS_IM)
    public int deleteGroupInfo(long id) {
        return groupMapper.deleteGroupInfo(id);
    }

    @UseDataSource(DataSourceType.YS_IM)
    public int deleteMembers(long id) {
        return groupMapper.deleteMembers(id);
    }

    @UseDataSource(DataSourceType.YS_IM)
    public GroupInfo groupInfo(long id){
        return groupMapper.groupInfo(id);
    }

    @UseDataSource(DataSourceType.YS_IM)
    public int updateName(long id, String name) {
        return groupMapper.updateName(id,name);
    }

    @UseDataSource(DataSourceType.YS_IM)
    public int greopUserDel(long uid,long groupId) {
        return groupMapper.greopUserDel(uid,groupId);
    }

    @UseDataSource(DataSourceType.YS_IM)
    public int updateUserNumber(long id, int number) {
        return groupMapper.updateUserNumber(id,number);
    }
}
